import React from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import {WeaLeftRightLayout, WeaLocaleProvider,WeaTools,WeaNewScroll,WeaOrgTree} from 'ecCom';
import {Menu, Tooltip} from 'antd';
import MyPlanView from './planReportView';
import PlanAuditList from './planAuditList/index';
import ProgramView from './programView/index';
import PlanReportDiagram from './planReportDiagram/index';
import ResultList from './planResultList/index';
import PlanView from './planView/index'
import * as Util from '../../util/Util'
const Item = Menu.Item;
const getLabel = WeaLocaleProvider.getLabel;

export default class PlanIndex extends React.Component {
  constructor (props) {
    super (props);
    this.state = {
      cunrrentkey: '1',
      auditUnread: 0,
      isRight:"",
      subcount:0,
      _height:Util.getCurrentHeight(),
      _selectKey:[],
      noAuditCount:0,
      _time:new Date()
    };
    window.planGetNoAuditCount=()=>{
      this.getNoAuditCount()
    }
  }
  componentWillMount () {
    //一些初始化请求
    WeaTools.callApi('/api/workrelate/common/checkHasRigth', 'GET', {id:4}).then((data) => {
        if(data.status && data.isRight){
          this.setState({isRight:"1",subcount:data.subcount});
          const menutype = Util.getParams('menutype');
          if(menutype){
            this.setState({cunrrentkey:menutype});
          }
        }else{
            this.setState({isRight:"0"});
        }
    })
    this.getNoAuditCount()
  } 
  handleClick = e => {
    this.setState ({cunrrentkey: e.key,_selectKey:[],_time:new Date()});
  };
  chooseUser(e){
    let hrmId = '', hrmName = undefined;
    if (e.selected) {
        hrmId = e.node.props.id;
        hrmName = e.node.props.name;
    }
    if(hrmId) {
      this.refs["planMenu"+this.state.cunrrentkey].wrappedInstance.onSubClick(hrmId,hrmName);
    }
  }
  onNodeSelect(ckey){
    this.setState ({_selectKey:ckey});
  }
  getNoAuditCount=()=>{
    WeaTools.callApi('/api/workrelate/plan/noAuditCount', 'POST', {}).then((res)=>{
      if (res.api_status) {
        this.setState({noAuditCount:res.noAuditCount})
      } else {
        message.error (res.msg || getLabel('383746','请求失败') );
      }
    })
  }
  render () {
    const {cunrrentkey, isRight,_height,subcount,_selectKey,noAuditCount,_time} = this.state;
    if(isRight==""){
        return <div></div>;
    }
    if(isRight=="0"){
        return Util.renderAlertPage('<span style="color:red">'+getLabel(509281,"您的系统未开启报告模块")+'</span>','','','',this.props.ecId || '');
    }
    let menu = (
      <Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@4o3ixa`}
        onClick={this.handleClick}
        style={{width: 248}}
        defaultOpenKeys={['1']}
        selectedKeys={[cunrrentkey]}
        mode="inline"
      >
        <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@xrnzwj`} key="1" title={getLabel('6015','我的报告') } style={{height:'30px',lineHeight:"30px"}}>
          <span>{getLabel (6015, '我的报告')}</span>
        </Item>
        <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@zomzdo`} key="2" title={getLabel('505884','报告审批') } style={{height:'30px',lineHeight:"30px"}}>
          <span>{getLabel('505884','报告审批') }</span>
          {noAuditCount == 0
            ? ''
            : <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@22psly`} title={getLabel('508869','${code}个待审批计划报告').replace("${code}",noAuditCount)}>
                <span className="workrelate-unread">{noAuditCount}</span>
              </Tooltip>}
        </Item>
        <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@60tm3x`} key="3" title={getLabel (505885, '报告查询')} style={{height:'30px',lineHeight:"30px"}}>
          <span>{getLabel (505885, '报告查询')}</span>
        </Item>
        <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@pf0978`} key="4" title={getLabel('16448','模板设置') } style={{height:'30px',lineHeight:"30px"}}>
          <span>{getLabel('16448','模板设置') }</span>
        </Item>
        <Item ecId={`${this && this.props && this.props.ecId || ''}_Item@2st1go`} key="5" title={getLabel (505886, '报告报表')} style={{height:'30px',lineHeight:"30px"}}>
          <span>{getLabel (505886, '报告报表')}</span>
        </Item>
        {/* <Item key="6" title={getLabel('6015','我的报告')} style={{height:'30px',lineHeight:"30px"}}>
          <span>{getLabel('6015','我的报告')}</span>
        </Item> */}
      </Menu>
    );
    let menumap = {
      '1': <PlanView ecId={`${this && this.props && this.props.ecId || ''}_PlanView@aidox9`} ref={"planMenu1"} key={_time} getNoAuditCount={this.getNoAuditCount}/>,
      '2': <PlanAuditList ecId={`${this && this.props && this.props.ecId || ''}_PlanAuditList@tpy48v`} ref={"planMenu2"} key={_time} getNoAuditCount={this.getNoAuditCount}/>,
      '3': <ResultList ecId={`${this && this.props && this.props.ecId || ''}_ResultList@kalx0c`} ref={"planMenu3"} key={_time}/>,
      '4': <ProgramView ecId={`${this && this.props && this.props.ecId || ''}_ProgramView@wrxyye`} ref={"planMenu4"} key={_time}/>,
      '5': <PlanReportDiagram ecId={`${this && this.props && this.props.ecId || ''}_PlanReportDiagram@kbq2tl`}/>,
      // '6': <MyPlanView ref={"planMenu6"} key={_time} getNoAuditCount={()=>this.getNoAuditCount()}/>,
    };
    return (
      <div className="wea-workrelate-plan-menu wea-workrelate-common-menu">
        <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@07a97d`}
          leftCom={<div className="common-menu-title">{menu}
          {cunrrentkey!=5 && subcount>0 && <div><div className="common-menu-subtitle">{getLabel(442,"下属")}</div>
          <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@q7nxbs`} height={_height-220}>
              <WeaOrgTree ecId={`${this && this.props && this.props.ecId || ''}_WeaOrgTree@cedfej`} dataUrl="/api/hrm/base/getHrmResourceTree" rootKey="rootManager"
                  treeNodeClick={(e)=>this.chooseUser(e)} selectedKeys={_selectKey} onNodeSelect={(ckey)=>this.onNodeSelect(ckey)}/>
          </WeaNewScroll></div>}
          </div>}
          leftWidth={248}
          onCollapse={showLeft => {}}>
          <div>{menumap[cunrrentkey]}</div>
        </WeaLeftRightLayout>
      </div>
    );
  }
}